<?php

/**
 * @Author:      zy
 * @DateTime:    2017-07-11 14:01:13
 * @Description: 废品操作
 */
class ScsManageController extends BaseController
{
    /**
     * @Author:      zy
     * @DateTime:    2017-07-11 14:01:13
     * @Description: 废品的详细信息
     */
    public function scrapInfo(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $scarp_type = ShopScrapType::get()->toArray();
            $start_time = date("Y-m-d 00:00:00");
            $end_time = date("Y-m-d 23:59:59");
            $shop_id = Input::get('shop_id');
            $scarp_list = ShopScrap::select(DB::raw('scrap_id , sum(amount) as all_amount,sum(num) as all_num'))
                    ->whereBetween("created_at",array($start_time,$end_time))
                    ->where('shop_id',$sc_user->shop_id)
                    ->groupBy('scrap_id')
                    ->get()->toArray();
            $list = [];
            foreach($scarp_type as $k=>$v){
                $list[$v['id']] = ['all_amount'=>0,'all_num'=>0];
            }
            $list['all'] = 0;
            $list['all_amount'] = 0;
            foreach ($scarp_list as $k=>$v) {
                $list[$v['scrap_id']] = ['scrap_id'=>$v['scrap_id'],'all_amount'=>round(bcdiv($v['all_amount'],100,6),2),'all_num'=>$v['all_num']];
                $list['all'] += $v['all_num'];
                $list['all_amount'] += round(bcdiv($v['all_amount'],100,6),2);
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array('scarp_type'=>$scarp_type,'scarp_list'=>$list));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zy
     * @DateTime:    2017-07-11 14:01:13
     * @Description: 添加废品信息
     */
    public function insertScrap(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $scrap_id = Input::get('scrap_id');
            $num = Input::get('num');
            $amount = Input::get('amount');
            $time = date("Y-m-d H:i:s");
            $date = date("Y-m-d");
            $amount = $amount*100;
            $array = ["id"=>'','scrap_id'=>$scrap_id,"shop_id"=>$sc_user->shop_id,"num"=>$num,"amount"=>$amount,'created_at'=>$time,'updated_at'=>$time,'collect_date'=>$date,'cretaed_by'=>$sc_user->id,'updated_by'=>$sc_user->id];
            $result = ShopScrap::insert($array);

            $response = array('code' => 0, 'msg' => 'ok', 'result' =>$result);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}